package com.es.test.utils;

/**
 * @author django
 * @date 2024/2/1
 * @description TODO
 */

import com.alibaba.excel.EasyExcel;

import com.es.test.dto.LabelInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

public class ReadExcelData {

/*    public static void main(String[] args) {
        String excelFilePath = "E:\\test\\test.xlsx";

        // 使用 EasyExcel 读取 Excel 文件
        List<LabelInfo> dataList = readExcel(excelFilePath);

        // 处理读取到的数据
        processReadData(dataList);
    }*/


    public static Map<String, List<String>> getExcelMap() {
        String excelFilePath = "/home/django.jiang/test.xlsx";

        // 使用 EasyExcel 读取 Excel 文件
        List<LabelInfo> dataList = readExcel(excelFilePath);

        // 处理读取到的数据
        return processReadData(dataList);
    }


    private static List<LabelInfo> readExcel(String filePath) {
        // 使用 EasyExcel 读取 Excel 文件
        List<LabelInfo> dataList = new ArrayList<>();

        // 通过 ExcelReaderBuilder 构建 ExcelReader

        EasyExcel.read(filePath, LabelInfo.class, new LabelInfoListener(dataList)).sheet().doRead();

        return dataList;
    }

    private static Map<String, List<String>> processReadData(List<LabelInfo> dataList) {
        HashMap<String, List<String>> fieldMap = new HashMap<>();
        dataList.forEach(item -> {
            // 使用反射获取对象的所有字段
            Field[] fields = item.getClass().getDeclaredFields();
            // 遍历字段，将字段名和字段值存入 Map
            for (Field field : fields) {
                field.setAccessible(true);

                try {
                    String fieldName = field.getName();

                    String value = (String) Optional.ofNullable(field.get(item)).orElse("");
                    if (fieldMap.get(fieldName) == null) {
                        List<String> list = new ArrayList<>();
                        fieldMap.put(fieldName, list);
                    }
                    if (StringUtils.isNotEmpty(value) && fieldMap.get(fieldName).contains(value) == false) {

                        if (value.length() < 40) {
                            String substring = value.substring(0, 1);
                            if (!NumberUtils.isDigits(substring)) {
                                String[] labels = value.split(" ");
                                if (labels.length >= 1) {
                                    fieldMap.get(fieldName).addAll(Arrays.stream(labels).collect(Collectors.toList()));
                                } else {
                                    String[] labelsa = value.split(",");
                                    if (labelsa.length >= 1) {
                                        fieldMap.get(fieldName).addAll(Arrays.stream(labelsa).collect(Collectors.toList()));
                                    } else {
                                        fieldMap.get(fieldName).add(value);
                                    }
                                }
                            } else {
                                fieldMap.get(fieldName).add(value);
                            }
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        return fieldMap;
    }

    public static void main(String[] args) {
        System.out.println("开朗 干练 培训 技术".split(" "));
    }

}
